﻿using System;
using System.Collections.Generic;
using System.Text;

namespace IOFileServer.ZMPhoneticEnglish.Implementation
{
    /// <summary>
    /// 实现类: 元音处理工具
    /// </summary>
    public class Vowel : IVowel
    {

        public Vowel()
        {
        }


        int[][] IVowel.CalcPosition(string word)
        {
            char[] word_arr = word.ToCharArray();
            return null;
        }

        public Rule ToRule(string letter_expression)
        {
            char[] letters = letter_expression.ToCharArray();
            IList<int> pindex_horizontal_bar = new List<int>();
            IList<int> pindex_brackets_left = new List<int>();
            IList<int> pindex_brackets_right = new List<int>();
            int left = -1, right = -1;
            IList<int[]> pindex_merges = new List<int[]>();
            for (int index = 0; index < letters.Length; index++)
            {
                char c = letters[index];
                bool is_special = false;
                switch (c)
                {
                    case '-':
                        pindex_horizontal_bar.Add(index);
                        is_special = true;
                        break;
                    case '(':
                        pindex_brackets_left.Add(index);
                        is_special = true;
                        break;
                    case ')':
                        pindex_brackets_right.Add(index);
                        is_special = true;
                        break;
                }
                if (is_special)
                {
                    if (left != -1)
                    {
                        pindex_merges.Add(new int[] { left, right });
                        left = -1;
                        right = -1;
                    }
                }
                else
                {
                    if (left == -1)
                        left = index;
                    right = index;
                }
            }
            if (left != -1)
            {
                pindex_merges.Add(new int[] { left, right });
            }

            if (pindex_horizontal_bar.Count > 2)
                throw new Exception("前后必要标识'-'多于2个");
            if (pindex_brackets_left.Count != pindex_brackets_right.Count)
                throw new Exception("左右括号'()'数量不对应");

            Rule r = new Rule()
            {
                letter = null,
                is_before_must = false,
                is_after_must = false,
                before_content = null,
                after_content = null,
            };

            int letter_merge_index;
            char[] selection_region(int[] position_indexs)
            {
                int left = position_indexs[0];
                int right = position_indexs[1];
                char[] rs = new char[right - left + 1];
                for (int i = 0; i < rs.Length; i++)
                {
                    rs[i] = letters[left + i];
                }
                return rs;
            }
            if (pindex_merges.Count == 1 && pindex_brackets_left.Count == 0)
            {
                r.letter = selection_region(pindex_merges[0]);
                letter_merge_index = 0;
            }
            else if (pindex_merges.Count == 2 && pindex_brackets_left.Count == 1)
            {
                int[] indexs = pindex_merges[0];
                int brackets_left = pindex_brackets_left[0];
                int brackets_right = pindex_brackets_right[0];

                if (brackets_left < indexs[0] && indexs[1] < brackets_right)
                {
                    r.before_content = selection_region(indexs);
                    r.letter = selection_region(pindex_merges[1]);
                    letter_merge_index = 1;
                }
                else
                {
                    r.letter = selection_region(indexs);
                    r.after_content = selection_region(pindex_merges[1]);
                    letter_merge_index = 0;
                }
            }
            else if (pindex_merges.Count == 3 && pindex_brackets_left.Count == 2)
            {
                r.before_content = selection_region(pindex_merges[0]);
                r.letter = selection_region(pindex_merges[1]);
                r.after_content = selection_region(pindex_merges[2]);
                letter_merge_index = 1;
            }
            else
            {
                throw new Exception($"无法识别的各部分数量: {pindex_merges.Count}, 括号对数量: {pindex_brackets_left.Count}");
            }

            int[] letter_merge_indexs = pindex_merges[letter_merge_index];
            for (int index_horizontal_bar = 0; index_horizontal_bar < pindex_horizontal_bar.Count; index_horizontal_bar++)
            {
                int horizontal_bar = pindex_horizontal_bar[index_horizontal_bar];
                r.is_before_must = r.is_before_must || horizontal_bar < letter_merge_indexs[0];
                r.is_after_must = r.is_after_must || letter_merge_indexs[1] < horizontal_bar;
            }

            return r;
        }

        public struct Rule
        {
            public char[] letter;
            public bool is_before_must;
            public bool is_after_must;
            public char[] before_content;
            public char[] after_content;
        }
    }
}
